package com.qwz.example;

import com.qwz.example.colleague.*;

/**
 * @author Qiuwuzhi
 * @version V1.0
 * @Description: 主板类，实现中介者接口
 * @Package com.qwz.example.MotherBoard
 * @date 2019/12/24 14:42
 */
public class MotherBoard implements Mediator {
    /**
     * 需要知道要交互的同事类-光驱
     */
    private CDDriver cdDriver;
    /**
     * 需要知道要交互的同事类-CPU
     */
    private CPU cpu;
    /**
     * 需要知道要交互的同事类-声卡
     */
    private SoundCard soundCard;
    /**
     * 需要知道要交互的同事类-显卡
     */
    private VideoCard videoCard;

    public void setCdDriver(CDDriver cdDriver) {
        this.cdDriver = cdDriver;
    }

    public void setCpu(CPU cpu) {
        this.cpu = cpu;
    }

    public void setSoundCard(SoundCard soundCard) {
        this.soundCard = soundCard;
    }

    public void setVideoCard(VideoCard videoCard) {
        this.videoCard = videoCard;
    }

    public void changed(Colleague colleague) {
        if (colleague == cdDriver) {
            this.opeCDDriverReadData((CDDriver) colleague);
        } else if (colleague == cpu) {
            this.opeCPU((CPU) colleague);
        }
    }

    private void opeCDDriverReadData(CDDriver driver) {
        //获取光驱读取的数据
        String data = cdDriver.getData();
        //把数据传递给CPU进行处理
        this.cpu.executeData(data);
    }

    private void opeCPU(CPU cpu) {
        String soundData = cpu.getSoundData();
        String videoData = cpu.getVideoData();

        this.videoCard.showData(videoData);
        this.soundCard.soundData(soundData);
    }
}
